package com.juku2024.juku.tools

import android.text.TextUtils
import java.text.SimpleDateFormat
import java.util.*

object TimeUtils {

    /**
     * 时间戳转换成日期格式字符串
     */
    fun timeStamp2Date(millis: Long): String {
        val format = "yyyy-MM-dd HH:mm:ss"
        val sdf = SimpleDateFormat(format, Locale.getDefault())
        return sdf.format(Date(millis))
    }

    /**
     * 时间戳转换成日期格式字符串
     */
    fun timeL2S(millis: Long): String {
        val format = "HH:mm:ss"
        val sdf = SimpleDateFormat(format, Locale.getDefault())
        return sdf.format(Date(millis))
    }

    /**
     * 时间戳转换成日期格式字符串
     */
    fun getSignTime(millis: Long): String {
        val format = "yyyy-MM-dd HH:mm"
        val sdf = SimpleDateFormat(format, Locale.getDefault())
        return sdf.format(Date(millis))
    }

    /**
     * long值转换时分秒
     */
    fun getHMSTime(time: Long): String {
        val timeStr: String?
        val hour: Long?
        var minute: Long?
        val second: Long?
        if (time <= 1000)
            return "00:00:00"
        else {
            val secondTime = time / 1000
            minute = secondTime / 60
            if (minute < 60) {
                second = secondTime % 60
                timeStr = "00:" +unitFormat(minute) + ":" + unitFormat(second)
            } else {
                hour = minute / 60
                if (hour > 99)
                    return "99:59:59"
                minute %= 60
                second = secondTime - (hour * 3600) - (minute * 60)
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second)
            }
        }
        return timeStr
    }

    /**
     * long值转换时分秒
     * 返回最大时间单位
     */
    fun getUnitTime(time: Long): String {
        val hour: Long?
        val hourStr: String
        var minute: Long?
        val minuteStr: String
        if (time <= 1000)
            return "0秒"
        else {
            val secondTime = time / 1000
            minute = secondTime / 60
            if (minute <= 0) {
                return  unitFormat(secondTime) + "秒"
            } else if (minute < 60) {
                minuteStr = unitFormat(minute)
                return (minuteStr + "分")
            } else {
                hour = minute / 60
                if (hour > 99)
                    return "99小时"
                minute %= 60
                hourStr = unitFormat(hour)
                return (hourStr + "小时")
            }
        }
    }


    private fun unitFormat(i: Long): String {
        return  if (i in 0..9)
            "0$i"
        else
            i.toString()
    }

    /**
     * 时间戳转换成日期格式字符串
     */
    fun feedbackTime(millis: Long): String {
        val format = "yyyy-MM-dd hh:mm"
        val sdf = SimpleDateFormat(format, Locale.getDefault())

        val timeFormat = sdf.format(Date(millis))
        val instance = Calendar.getInstance()
        instance.timeInMillis = millis
        val hour = instance.get(Calendar.HOUR_OF_DAY)
        val time = if (hour > 12) {
            "下午"
        } else {
            "上午"
        }
        if (TextUtils.isEmpty(timeFormat) || timeFormat.length < 10) {
            return ""
        }
        val substring1 = timeFormat.substring(0, 11)
        val substring2 = timeFormat.substring(11)
        return substring1 + time + substring2

    }
}